package cn.tedu.javagame.ball;

import java.awt.Color;

public class Brick {
    int x, y, width, height;
    Color color;
    boolean isVisible = true; // 新增字段，表示方块是否可见

    // 构造函数，初始化方块的位置、大小和颜色
    public Brick(int x, int y, int width, int height, Color color) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        this.color = color;
    }



    // 碰撞检测方法，判断球是否与方块碰撞
    public boolean isHitBy(Ball ball) {
        return ball.x + ball.length >= x &&
                ball.x <= x + width &&
                ball.y + ball.length >= y &&
                ball.y <= y + height;
    }

    // 处理球与方块碰撞的方法，改变球的状态
    public void handleCollision(Ball ball) {
        if (isHitBy(ball)) {
             if (ball.y + ball.length >= y + height) {
                // 球从上下两侧碰撞方块，改变球的垂直方向
                if (ball.state == 0) {
                    ball.state = 2;
                } else if (ball.state == 1) {
                    ball.state = 3;
                }
            }
            // 如果球和方块颜色相同，将方块设置为不可见
            if (ball.color.equals(this.color)) {
                this.isVisible = false;
            }
        }
    }

    // 获取方块是否可见
    public boolean isVisible() {
        return isVisible;
    }

    // 设置方块是否可见
    public void setVisible(boolean visible) {
        isVisible = visible;
    }
}